package com.foreknow.elm.service.impl;
import com.foreknow.elm.dao.DeliveryAddressDao;
import com.foreknow.elm.dao.impl.DeliveryAddressDaoImpl;
import com.foreknow.elm.po.DeliveryAddress;
import com.foreknow.elm.service.DeliveryAddressService;
import com.foreknow.elm.util.DBUtil;
import java.util.ArrayList;
import java.util.List;
public class DeliveryAddressServiceImpl implements DeliveryAddressService {
    private int a;
    @Override
    public List<DeliveryAddress> listDeliveryAddressByUserId(String userId) {
        List<DeliveryAddress> list = new ArrayList<>();
        DeliveryAddressDao dao = new DeliveryAddressDaoImpl();
        try {
            //获取连接
            DBUtil.getConnection();
            list = dao.listDeliveryAddressByUserId(userId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return list;

    }

    @Override
    public DeliveryAddress getDeliveryAddressById(Integer daId) {
        DeliveryAddress deliveryAddress = null;
        DeliveryAddressDao dao = new DeliveryAddressDaoImpl();
        try {
            // 获取连接
            DBUtil.getConnection();
            deliveryAddress = dao.getDeliveryAddressById(daId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return deliveryAddress;
    }

    @Override
    public int saveDeliveryAddress(DeliveryAddress deliveryAddress) {
        DeliveryAddressDao dao = new DeliveryAddressDaoImpl();
        try {
            // 开启事务
            DBUtil.beginTransaction();
            // 获取连接
            DBUtil.getConnection();
            a = dao.saveDeliveryAddress(deliveryAddress);
            // 提交事务
            DBUtil.commitTransaction();
        } catch (Exception e) {
            try {
                // 有异常时回滚事务
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return a;
    }

    @Override
    public int updateDeliveryAddress(DeliveryAddress deliveryAddress) {
        DeliveryAddressDao dao = new DeliveryAddressDaoImpl();
        try {
            // 开启事务
            DBUtil.beginTransaction();
            // 获取连接
            DBUtil.getConnection();
            a = dao.updateDeliveryAddress(deliveryAddress);
            DBUtil.commitTransaction();
        } catch (Exception e) {
            try {
                // 有异常时回滚事务
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return a;
    }

    @Override
    public int removeDeliveryAddress(Integer daId) {
        DeliveryAddressDao dao = new DeliveryAddressDaoImpl();
        try {
            // 开启事务
            DBUtil.beginTransaction();
            // 获取连接
            DBUtil.getConnection();
            a = dao.removeDeliveryAddress(daId);
            // 提交事务
            DBUtil.commitTransaction();
        } catch (Exception e) {
            try {
                // 有异常时回滚事务
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return a;
    }
}
